<?php

class Technical_ExamstypeController extends Zend_Controller_Action
{

	public function init()
	{
		/* Initialize action controller here */
	}

	public function indexAction()
	{
		$session = new Zend_Session_Namespace('identity');
		$examsTypes = new Application_Model_Dao_ExamsType();
		$result = $examsTypes->getAll()->toArray();
		$this->view->examsTypes = $result;
		if($result == null){
			$this->view->empty = "Brak rodzaju studiów w bazie.";
		}else{
			$this->view->empty = "";
		}
		$designation = $this->_getParam('designation');
		if(isset($session->statusdel) && $this->_getParam('statusdelete') == 'true') {
			$this->view->delete = "Rekord \"".$designation."\" został poprawnie usunięty.";
			unset($session->statusdel);
		}else{
			$this->view->delete = "";
		}

		if( isset( $session->attention ) ) {
			$this->view->attention = $session->attention ;
			unset( $session->attention );
		} else {
			$this->view->attention = "";
		}

		if( isset( $session->updatesuccess ) ) {
			$this->view->updateStatus = $session->updatesuccess ;
			unset( $session->updatesuccess );
			if(isset( $session->updaterecorddesignation ) ) {
				$this->view->updateRecordDesignation = $session->updaterecorddesignation;
				unset( $session->updaterecorddesignation );
			} else {
				$this->view->updateRecordDesignation = "";
			}
		} else if( isset($session->deletesuccess) ) {
			$this->view->deleteStatus = $session->deletesuccess;
			unset($session->deletesuccess);
			if(isset( $session->deleterecorddesignation ) ) {
				$this->view->deleteRecordDesignation = $session->deleterecorddesignation;
				unset( $session->deleterecorddesignation );
			} else {
				$this->view->deleteRecordDesignation = "";
			}
		} else if( isset($session->addsuccess) ) {
			$this->view->addStatus = $session->addsuccess;
			unset($session->addsuccess);
			if(isset( $session->addrecorddesignation ) ) {
				$this->view->addRecordDesignation = $session->addrecorddesignation;
				unset( $session->addrecorddesignation );
			} else {
				$this->view->addRecordDesignation = "";
			}
		} else {
			$this->view->deleteRecord = "";
		}
		$session = new Zend_Session_Namespace('identity');
		$form = new Application_Form_ExamsType();
		$form->appendValidationEngineFiles($this->view);
		$polish = BH_TranslatePL::getPolishTranslation();
		$translate = new Zend_Translate('array', $polish, 'pl');
		$form->setTranslator($translate);
		// edycja oraz dodawanie w jquery, kod obsługujący requesta z danymi z formularza, zapis do bazy
		if( $this->getRequest()->isPost() ) {
			$formData = $this->getRequest()->getPost();
			if( $form->isValid($formData) ) {
				$examsType = new Application_Model_Entities_ExamsType();
				$examsType->setId($form->getValue('id'));
				$examsType->setDesignation($form->getValue('designation'));
				$examsType->setShort($form->getValue('short'));
				$examsTypeDao = new Application_Model_Dao_ExamsType();
				if($form->getValue('typeForm') == 'editform' ) {
					//print_r($coursesType);
					$result = $examsTypeDao->updateExamsType($examsType);
					if($result > 0 ) {
						$session->updatesuccess = 'updatesuccess';
					} else {
						$session->updatesuccess = 'updatefail';
					}
				} else if ($form->getValue('typeForm') == 'addform' ) {
					$result = $examsTypeDao->saveExamsType($examsType);
					if($result > 0 ) {
						$session->addsuccess = 'addsuccess';
						$session->addrecorddesignation = $form->getValue('designation');
					} else {
						$session->addsuccess = 'addfail';
					}
				} else {
					$session->attention = 'Błędne dane dotyczące typu operacji';
				}
				$this->_helper->redirector('index');
			} else {
				$form->populate($formData);
			}
		} else {
			$session->attention = '';
		}

	}

	public function addAction(){
		$session = new Zend_Session_Namespace('identity');
		$form = new Application_Form_ExamsType();
		$form->appendValidationEngineFiles($this->view);
		$polish = BH_TranslatePL::getPolishTranslation();
		$translate = new Zend_Translate('array', $polish, 'pl');
		$form->setTranslator($translate);
		$form->getElement('typeForm')->setValue('addform');
		$form->submit->setLabel('Dodaj nowy rodzaj zaliczenia');
		$this->view->ExamsTypeForm = $form;
		$request = $this->getRequest();
		if ($request->isPost()) {
			if ($form->isValid($request->getPost())) {
				$examsType = new Application_Model_Entities_ExamsType();
				$examsType->setDesignation($form->getValue('designation'));
				$examsType->setShort($form->getValue('short'));
				$examsTypeDao = new Application_Model_Dao_ExamsType();
				$examsTypeDao->saveExamsType($examsType);
				$this->_helper->redirector('index');
			} else {
				$session->attention = 'Przesłane dane w formularzu nie są poprawne.';
				$this->_helper->redirector( 'index');
			}
		} else {
			$session->attention = 'Formularz prawdopodobnie nie został wysłany.';
			//$this->_helper->redirector( 'index');
		}
	}
	public function editAction(){
		$session = new Zend_Session_Namespace('identity');
		$form = new Application_Form_ExamsType();
		$form->appendValidationEngineFiles($this->view);
		$polish = BH_TranslatePL::getPolishTranslation();
		$translate = new Zend_Translate('array', $polish, 'pl');
		$form->setTranslator($translate);
		$form->submit->setLabel('Zatwierdź zmiany');
		$this->view->ExamsTypeEditForm = $form;
		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {
				$examsType = new Application_Model_Entities_ExamsType();
				$examsType->setDesignation( $form->getValue('designation'));
				$examsType->setShort($form->getValue('short'));
				$examsTypeDao = new Application_Model_Dao_ExamsType();
				$examsTypeDao->updateExamsType($examsType);
				$this->_helper->redirector('index');
			} else {
				$form->populate($formData);
			}
		} else {
			$id = $this->_getParam('id', 0);
			if ($id > 0) {
				$examsTypeDao = new Application_Model_Dao_ExamsType();
				$form->populate($examsTypeDao->getExamsTypeByID($id));
				$session->updaterecorddesignation = $form->getValue('designation');
				$form->getElement('typeForm')->setValue('editform');
			} else {
				$session->attention = 'Błąd podczas pobierania danych lub podczas przypisywania ich do pól formularza.';
				$this->_helper->redirector( 'index');
			}
		}

	}
	public function deleteAction(){
		$session = new Zend_Session_Namespace('identity');
		$request = $this->getRequest();
		if ($request->isPost()) {
			$del = $request->getPost('del');
			if ($del == 'Tak, usuń rekord') {
				$id = $request->getPost('id');
				$designation = $request->getPost('designation');
				$examsType = new Application_Model_Entities_ExamsType();
				$examsType->setId($id);
				$examsTypeDao = new Application_Model_Dao_ExamsType();
				$examsTypeDao->deleteExamsType($examsType);
				$session->statusdel = 'true';
				$this->_helper->redirector( 'index', 'examstype', 'technical', array('statusdelete' => 'true', 'designation' => $designation));
			} else {
				$this->_helper->redirector( 'index');
			}
		} else if($this->_getParam('jsflag') == 'false' ){
			$id = $this->_getParam('id', 0);
			if ($id > 0) {
				$examsTypeDao = new Application_Model_Dao_ExamsType();
				$result = $examsTypeDao->getRowExamsType($id);
				$this->view->examsTypes = $result;
			}
		} else if($this->_getParam('jsflag') == 'true' ){
			$id = $this->_getParam('id', 0);
			$examsType = new Application_Model_Entities_ExamsType();
			$examsType->setId($id);
			$examsTypeDao = new Application_Model_Dao_ExamsType();
			$result = $examsTypeDao->deleteExamsType($examsType);
			if($result > 0) {
				$session->deletesuccess = "deletesuccess" ;
				$session->deleterecorddesignation = $this->_getParam('name', 0);
			} else {
				$session->deleterecorddesignation = "";
				$session->deletesuccess = "deletefail" ;
			}

		} else {
			$this->_helper->redirector('index');
		}
	}

}
	
